home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / numerics / presvalu.sx
Encoding:
Text File  |  1996-05-21  |  3.0 KB  |  90 lines  |  [TEXT/ttxt]

  1. -- <<<-
  2. module ValueTest1 uses ScriptX end
  3. in module ValueTest1
  4.  
  5. class Payment (Pair)
  6.     instance methods
  7.         method add self key myValue -> (
  8.             -- specialize the add method to do type checking
  9.             if ((self.size = 0) and (not getClass myValue = Date)) do (
  10.                 format debug "first element not a date\n" undefined @normal
  11.             ) 
  12.             if ((self.size = 1) and (not isAKindOf myValue Number)) do (
  13.                 format debug "second element not a number\n" undefined @normal
  14.             )
  15.             apply nextMethod self key myValue
  16.         )
  17.         method presentValue self today discountRate -> (
  18.             if not isAKindOf discountRate Number do (
  19.                 format debug "third arg not a number\n" undefined @normal
  20.             )
  21.             -- handles dates that have already been converted to large integer
  22.             if (getClass today = Date) then (
  23.                 -- there are (60 * 60 * 24 * 365.25) seconds in a year
  24.                 local secondsPerYear := 31557600 
  25.                 local y := (today as LargeInteger)/secondsPerYear
  26.                 local z := (self[1] as LargeInteger)/secondsPerYear
  27.                 -- return the present value of the payment
  28.                 return (self[2] * exp (negate(discountRate * (z - y))))            
  29.             ) else (
  30.                 format debug "second arg not a date\n" undefined @normal
  31.             )
  32.         )
  33.         method prin self arg stream -> (
  34.             prin "You will receive $" @unadorned debug
  35.             prin self[2] @unadorned debug; prin " on " @unadorned debug
  36.             prin (self[1] as String) @unadorned debug; prin "\n" @unadorned debug
  37.         )
  38. end -- Payment
  39.  
  40. class PaymentList (IndirectCollection)
  41.     instance methods
  42.         method init self #rest args -> (
  43.             apply nextMethod self targetCollection:(new SortedArray) args
  44.         )
  45.         -- this method is called automatically before an object is added
  46.         -- the object does not get added to the collection unless it returns true
  47.         method isAppropriateObject self addedObject -> (
  48.             -- check that it is the right kind of object
  49.             if not isAKindOf addedObject Payment then (
  50.                 format debug "not a pair\n" undefined @normal
  51.                 return false
  52.             ) else (
  53.                 return true
  54.             )
  55.         )
  56.         -- this method is called automatically after an object is added
  57.         method objectAdded self key obj -> (
  58.             format debug "Your total is %*\n" (self.total) @normal
  59.         )
  60.         -- total is an example of a virtual instance variable
  61.         method totalGetter self -> (
  62.             local sum := 0
  63.             forEach self (x -> sum := sum + x[2]) undefined
  64.             return sum
  65.         )
  66.         -- generic prin takes care of all printing functions
  67.         method prin self arg stream -> (
  68.             forEach self (x->prin x @unadorned debug) undefined
  69.         )
  70.         method netPresentValue self discountRate -> (
  71.             local sum := 0 -- initialization
  72.             local today := theCalendarClock.date
  73.             -- calls a function, defined inline, on each member of the collection
  74.             forEach self (x -> sum := \
  75.                     sum + presentValue x today discountRate) undefined
  76.             return sum
  77.         )
  78. end -- PaymentList
  79.  
  80. -- create an instance, and then add some data to it
  81. object myWinnings (PaymentList) end
  82. for i in 1 to 20 do (
  83.     add myWinnings empty (new Payment \
  84.             values:#(new Date year:(1995 + i) month:@january,1000000))
  85. )
  86. netPresentValue myWinnings 0.06
  87. netPresentValue myWinnings 0.10
  88.  
  89. -->>>
  90.